//
//  QDUnionFillHeaderView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/6.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 每行的文本框
final class QDUnionFillTextField: UIView {
    
    var text: String? {
        return textField.text
    }
    /// 显示描述标题
    let titleLabel = UILabel()
    /// 文本框
    let textField = UITextField()
    /// 底部线
    let lineView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        titleLabel.textColor = QDColors.separator
        titleLabel.font = Font.light.font(size: 14)
        
        textField.font = Font.semibold.font(size: 14)
        textField.textColor = QDColors.separator
        textField.clearButtonMode = .whileEditing
        textField.tintColor = QDColors.theme
        
        lineView.backgroundColor = 235.ritl_color
        
        addSubview(titleLabel)
        addSubview(textField)
        addSubview(lineView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(7)
            make.left.equalToSuperview().offset(20)
            make.height.equalTo(20)
        }
        
        textField.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLabel)
            make.left.equalToSuperview().offset(100)
            make.height.equalTo(20)
            make.right.equalToSuperview().inset(20).priority(.high)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.height.equalTo(0.5)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().inset(20).priority(.high)
            make.bottom.equalToSuperview()
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

/// 银联信息填写
final class QDUnionFillHeaderView: UIView {

    /// 底部的图片
    private let backgroundView = UIImageView()
    /// 真实姓名
    private let nameTextField = QDUnionFillTextField()
    /// 身份证号
    private let idCardTextField = QDUnionFillTextField()
    /// 银行卡号
    private let bankTextField = QDUnionFillTextField()
    /// 手机好
    private let phoneTextField = QDUnionFillTextField()
    
    private let titles = ["真实姓名","身份证号","银行卡号","手机号码"]
    private let placeholders = ["请输入真实姓名","请输入身份证号","请输入银行卡号","请输入银行预留手机号"]
    private var textFields: [QDUnionFillTextField]{
        get {
            return [nameTextField,idCardTextField,bankTextField,phoneTextField]
        }
    }
    
    
    /// 数据的内容
    var texts: [(String,String)] {
        get {
            return [("name",nameTextField.text ?? ""),
                    ("id",idCardTextField.text ?? ""),
                    ("bank",bankTextField.text ?? ""),
                    ("phone",phoneTextField.text ?? "")]
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundView.image = "gen_background".namedImage?.resizableImage(withCapInsets: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
        addSubview(backgroundView)
        backgroundView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(13).priority(.high)
            make.right.bottom.equalToSuperview().inset(13)
        }
        
        //进行textField的设置
        for (index,fillView) in textFields.enumerated() {

            //进行设置
            fillView.textField.attributedPlaceholder = NSAttributedString(string: placeholders[index], attributes: [.font : Font.light.font(size: 14),.foregroundColor : #colorLiteral(red: 0.7607843137, green: 0.768627451, blue: 0.7647058824, alpha: 1)])
            fillView.titleLabel.text = titles[index]
            addSubview(fillView)
            
            let height = 34
            let topSpace = 38
            let itemSpace = 20
            
            fillView.snp.makeConstraints { (make) in
                make.height.equalTo(height)
                make.left.equalToSuperview().offset(20)
                make.right.equalToSuperview().inset(20).priority(.high)
                make.top.equalToSuperview().offset(topSpace + (height + itemSpace) * index)
            }
        }
        
        bankTextField.textField.keyboardType = .numberPad
        phoneTextField.textField.keyboardType = .numberPad
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
